var config = require('../../../utils/config.js')
var util = require('../../../utils/util.js')
var city = require('../../../utils/city.js')
var API_URL = config.API_URL
var app = getApp();
var name, value;
var setBarTitle = function (title) {
    my.setNavigationBar({
        title: title
    })
}
// let updata_user = () => {
//     let params = {
//         type: name,
//         value: value
//     }
//     my.httpRequest({
//         url: API_URL.edit_user, // 目标服务器 url
//         data: params,
//         success: (res) => {
//             console.log(res)
//             my.showToast({
//                 content: '修改成功',
//                 duration: 800
//             })
//             setTimeout(function () {
//                 my.navigateBack({
//                     delta: 1
//                 })
//             }, 800)
//         },
//     });
// }
let updata_user = () => {
    let params = {
        type: name,
        value: value
    }
    util.PRL(API_URL.edit_user, params)
        .then(res => {
            my.showToast({
                content: '修改成功',
                duration: 800
            })
            setTimeout(function () {
                my.navigateBack({
                    delta: 1
                })
            }, 800)
        })
}
Page({
    data: {
        showAddr: false,
    },
    onLoad: function (options) {
        console.log(options)
        // 生命周期函数--监听页面加载
        var that = this;
        name = options.name;
        // 命名的值
        value = options.value;
        city.init(that);
        switch (name) {
            case 'nickname': setBarTitle('修改昵称'); that.setData({ data: value }); break;
            case 'sign': setBarTitle('修改个性签名'); that.setData({ data: value }); break;
            case 'region_id': setBarTitle('修改地区'); that.setData({ showAddr: true, region_name: value }); break;
        }
    },
    onReady: function () {
        // 生命周期函数--监听页面初次渲染完成
    },
    onShow: function () {
        // 生命周期函数--监听页面显示
    },
    onHide: function () {
        // 生命周期函数--监听页面隐藏
    },
    onUnload: function () {
        // 生命周期函数--监听页面卸载
    },
    onPullDownRefresh: function () {
        // 页面相关事件处理函数--监听用户下拉动作
    },
    onReachBottom: function () {
        // 页面上拉触底事件的处理函数
    },
    save: function (e) {
        console.log(e);
        value = e.detail.value.data;
        console.log(value)
        updata_user();
    },
    bindChange: function (e) {
        const current_value = e.detail.value;
        console.log(current_value)
        city.change(current_value, this);
        var data = this.data;
        this.setData({
            region_name: data.provice + data.city + data.district
        })
    },
    saveAddr: function (e) {
        value = this.data.regionId;
        updata_user();
    }
})